/*
 * 
 * 
 * 
 */
package com.hboxs.asl.controller.admin;

import com.hboxs.asl.Message;
import com.hboxs.asl.Pageable;
import com.hboxs.asl.entity.Merchant;
import com.hboxs.asl.service.MerchantService;
import com.hboxs.asl.util.CodeUtils;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 * Controller - 商家
 */
@Controller("adminMerchantController")
@RequestMapping("/asl-admin/merchant")
public class MerchantController extends BaseController {

    @Resource(name = "merchantServiceImpl")
    private MerchantService merchantService;

    /**
     * 添加
     */
    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String add(ModelMap model, HttpSession session) {
        return "/admin/merchant/add";
    }

    /**
     * 保存
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public String save(Merchant merchant, HttpServletRequest request, RedirectAttributes redirectAttributes) {
        if (!isValid(merchant)) {
            return ERROR_VIEW;
        }
        merchant.setRegisterIp(request.getRemoteAddr());
        merchant.setPassword(DigestUtils.md5Hex(merchant.getPassword()));
        merchant.setLoginIp(null);
        merchant.setPoint(new BigDecimal(0));
        merchant.setLoginDate(null);
        merchant.setIsLocked(false);
        merchant.setLoginFailureCount(0);
        merchant.setBalance(new BigDecimal(0));
        merchantService.save(merchant);
        merchant.setCode(CodeUtils.generateCode(merchant));
        merchantService.update(merchant);
        addFlashMessage(redirectAttributes, SUCCESS_MESSAGE);
        return "redirect:list.htm";
    }

    /**
     * 编辑
     */
    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    public String edit(Long id, ModelMap model, HttpSession session) {
        model.addAttribute("merchant", merchantService.find(id));
        return "/admin/merchant/edit";
    }

    /**
     * 更新
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public String update(Merchant merchant, RedirectAttributes redirectAttributes) {
        if (!isValid(merchant)) {
            return ERROR_VIEW;
        }

        Merchant pMerchat = merchantService.find(merchant.getId());
        if (pMerchat == null) {
            return ERROR_VIEW;
        }

        if (StringUtils.isEmpty(merchant.getPassword())) {
            merchant.setPassword(pMerchat.getPassword());
        } else {
            merchant.setPassword(DigestUtils.md5Hex(merchant.getPassword()));
        }

        if (pMerchat.getIsLocked() && !merchant.getIsLocked()) {
            merchant.setLoginFailureCount(0);
            merchant.setLockedDate(null);
        } else {
            merchant.setIsLocked(pMerchat.getIsLocked());
            merchant.setLoginFailureCount(pMerchat.getLoginFailureCount());
            merchant.setLockedDate(pMerchat.getLockedDate());
        }

        merchantService.update(merchant, "point", "username", "registerIp", "loginIp", "loginDate", "code", "balance");
        addFlashMessage(redirectAttributes, SUCCESS_MESSAGE);
        return "redirect:list.htm";
    }

    /**
     * 列表
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String list(Pageable pageable, ModelMap model) {
        model.addAttribute("page", merchantService.findPage(pageable));
        return "/admin/merchant/list";
    }

    /**
     * 删除
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public
    @ResponseBody
    Message delete(Long[] ids) {
        merchantService.delete(ids);
        return SUCCESS_MESSAGE;
    }

}
